<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#overProductionTable', '超产数据')"
          v-hasPermi="['production:overProduction:export']"
        >
          导出
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      id="overProductionTable"
      v-loading="loading"
      :data="sgList"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column
        label="生产日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="moeScrq"
        width="90"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.moeScrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="机台"
        :show-overflow-tooltip="true"
        width="75"
        align="center"
        prop="moeJtbh"
      />
      <el-table-column
        label="制造单号"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="moeZzdh"
      />
      <el-table-column
        label="销售单号"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="moeSodh"
      />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="moeWldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        min-width="150"
        align="center"
        prop="itmPmgg"
      />
      <el-table-column
        label="模具编号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="moeMjbh"
      />
      <el-table-column
        label="计划数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moeScsl"
      />
      <el-table-column
        label="良品数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moeLpsl"
      />
      <el-table-column
        label="超产数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="moeCcsl"
      />
      <el-table-column
        label="超产率"
        :show-overflow-tooltip="true"
        width="90"
        align="center"
        prop="moeRate"
      />
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="生产日期">
            <el-date-picker
              v-model="daterangeRq"
              :picker-options="pickerOptions"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="销售单号" prop="moeSodh">
            <el-input
              v-model="queryParams.moeSodh"
              placeholder="请输入销售单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 240px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="物料代码" prop="moeWldm">
            <el-input
              v-model="queryParams.moeWldm"
              placeholder="请输入物料代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 240px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listOverProduction } from "@/api/production/overProduction";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "OverProduction",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // 刷卡时产生的数据表格数据
      sgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 生产日期时间范围
      // 开单日期时间范围
      daterangeRq: [
        new Date(new Date().getTime() - 3600 * 1000 * 24 * 30),
        new Date(new Date().getTime() + 3600 * 1000 * 24 * 60),
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        moeSodh: null,
        moeWldm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询刷卡时产生的数据列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRq && "" != this.daterangeRq) {
        this.queryParams.params["beginDate"] = this.moment(
          this.daterangeRq[0]
        ).format("YYYY-MM-DD");
        this.queryParams.params["endDate"] = this.moment(
          this.daterangeRq[1]
        ).format("YYYY-MM-DD");
      }
      this.queryParams.pageNum = 1;
      listOverProduction(this.queryParams).then((response) => {
        this.sgList = [];
        this.sgList = this.sgList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        listOverProduction(this.queryParams).then((response) => {
          this.sgList = this.sgList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        moeSodh: null,
        moeWldm: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.sgList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSearch() {
      this.reset();
      this.title = "超产查询";
      this.openSearch = true;
    },
    /** 导出按钮操作 */
    handleExport(a, b) {
      exportTable(a, b);
    },
  },
};
</script>

<style lang="scss" scoped></style>
